//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public abstract class NumericReference : NumericDataSource
    {
        public abstract string Formula { get; }

        protected NumericLiteral Cache { get; set; }

        internal static NumericReference ReadNumericReference(ReadContext context)
        {
            var factory = ReadContextData.GetReferenceFactory(context);
            NumericReference numRef = factory.CreateNumericReference();
            numRef.ReadXmlCore(context);
            return numRef;
        }


        protected override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("numRef"))
            {
                c.ReadElementString("f");
                Cache = c.ReadOptionalElementAs<NumericLiteral>("numCache");
                if (Cache != null)
                    Cache.Parent = this;
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }


        protected override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("numRef"))
            {
                c.WriteElementString("f", Formula);
                BuildCache();
                c.WriteOptionalElementAs(Cache, "numCache");
                c.WriteOptionalElement(Extension);
            }
        }

        protected virtual void BuildCache()
        {
        }
    }
}